package com.dmap.admin.controller.background;

import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.background.param.RoleParam;
import com.dmap.admin.po.AdminPermissions;
import com.dmap.admin.po.AdminRole;
import com.dmap.admin.service.backgroundv1.RolePermissionService;
import com.dmap.admin.service.backgroundv1.RoleService;
import com.dmap.admin.vo.PermissionVo;
import com.dmap.admin.vo.RoleVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 08/12/17 Time: 10:56
 * \* Description:
 * \
 */

@Api(value = "角色管理", description = "background/角色管理")
@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v1/user/role/")
public class RoleManageController {

    @Resource
    private RoleService roleService;
    @Resource
    private RolePermissionService rolePermissionService;

    @ResponseBody
    @ApiOperation(value = "查询角色列表", notes = "查询角色列表, 及该角色对应的权限列表")
    @RequestMapping(value = "select", method = RequestMethod.GET)
    public BaseVo select() {
        BaseVo baseVo = new BaseVo();
        List<AdminRole> adminRoleList = roleService.selectRoleList();
        if (CollectionUtils.isEmpty(adminRoleList)) {
            return baseVo;
        }
        List<RoleVo> roleVoList = new ArrayList<>();
        for (AdminRole adminRole : adminRoleList) {
            RoleVo roleVo = new RoleVo(adminRole);
            List<AdminPermissions> adminPermissionsList = rolePermissionService.selectPermissionListByRoleId(adminRole.getId());
            List<PermissionVo> permissionVoList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(adminPermissionsList)) {
                for (AdminPermissions adminPermissions : adminPermissionsList) {
                    permissionVoList.add(new PermissionVo(adminPermissions));
                }
            }
            roleVo.setPermissionVoList(permissionVoList);
            roleVoList.add(roleVo);
        }
        baseVo.setData(roleVoList);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "add 角色", notes = "必填： name, 返回值为：id")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public BaseVo insert(@RequestBody RoleParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getName())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        AdminRole adminRole = roleService.insert(param);
        if (adminRole == null) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
            return baseVo;
        }
        baseVo.setData(adminRole.getId().toString());
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "update 角色", notes = "必填： id, name")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseVo update(@RequestBody RoleParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getId()) || StringUtils.isBlank(param.getName())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        AdminRole adminRole01 = roleService.selectRoleById(Long.valueOf(param.getId()));
        if (adminRole01 == null) {
            baseVo.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
            return baseVo;
        }
        if (adminRole01.getName().equals(AdminConstant.SUPER_ADMIN_ROLE)) {
            baseVo.setCode(AdminConstant.RESPONSE_OPERATE_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_OPERATE_ERROR_MSG);
            return baseVo;
        }
        AdminRole adminRole = roleService.update(param);
        if (adminRole == null) {
            baseVo.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
            return baseVo;
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "delete 角色 by id", notes = "require id")
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public BaseVo delete(@RequestParam(name = "id") String id) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(id)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        AdminRole adminRole = roleService.selectRoleById(Long.valueOf(id));
        if (adminRole == null) {
            baseVo.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
            return baseVo;
        }
        if (adminRole.getName().equals(AdminConstant.SUPER_ADMIN_ROLE)) {
            baseVo.setCode(AdminConstant.RESPONSE_OPERATE_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_OPERATE_ERROR_MSG);
            return baseVo;
        }
        if (roleService.delete(Long.valueOf(id))) {
            return baseVo;
        }
        baseVo.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
        baseVo.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "根据id查询角色", notes = "必填：id")
    @RequestMapping(value = "select/selectRoleById", method = RequestMethod.GET)
    public BaseVo selectRoleById(@RequestParam String id) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(id)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        AdminRole adminRole = roleService.selectRoleById(Long.valueOf(id));
        if (adminRole == null) {
            return baseVo;
        }
        RoleVo roleVo = new RoleVo(adminRole);
        baseVo.setData(roleVo);
        return baseVo;
    }

}
